﻿
Imports System.Security.Permissions

Public Interface IMapControl
    WriteOnly Property AlbumManager() As AlbumManager
    WriteOnly Property Connection() As Connection
    Property ShowControlsOnMap() As Boolean
    WriteOnly Property AutoResize() As Boolean
    Event PhotoClicked(ByVal VE_shapeid As String, ByVal row As DataRow)
    Event PhotoDoubleClicked(ByVal VE_shapeid As String, ByVal row As DataRow)
    Event LoadingPhotos()
    Event LoadComplete()
    Event WebpageError(ByVal message As String, ByVal url As String, ByVal line As Integer)
    Event LoadingWebpage()
End Interface

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class VirtualEarthMapControl
    Inherits System.Windows.Forms.UserControl
    Implements IMapControl

    Private WithEvents m_album_mgr As AlbumManager
    Private WithEvents m_connection As Connection
    Private m_showcontrols As Boolean = True
    Private m_map_loaded As Boolean = False
    Private m_autoresize As Boolean = True

    Public Event PhotoClicked(ByVal VE_shapeid As String, ByVal row As DataRow) Implements IMapControl.PhotoClicked
    Public Event PhotoDoubleClicked(ByVal VE_shapeid As String, ByVal row As DataRow) Implements IMapControl.PhotoDoubleClicked
    Public Event WebpageError(ByVal message As String, ByVal url As String, ByVal line As Integer) Implements IMapControl.WebpageError
    Public Event LoadingWebpage() Implements IMapControl.LoadingWebpage
    Public Event LoadingPhotos() Implements IMapControl.LoadingPhotos
    Public Event LoadComplete() Implements IMapControl.LoadComplete

#Region " properties "
    Public WriteOnly Property Connection() As Connection Implements IMapControl.Connection
        Set(ByVal value As Connection)
            m_connection = value
        End Set
    End Property
    Public WriteOnly Property AlbumManager() As AlbumManager Implements IMapControl.AlbumManager
        Set(ByVal value As AlbumManager)
            m_album_mgr = value
        End Set
    End Property
    Public Property ShowControlsOnMap() As Boolean Implements IMapControl.ShowControlsOnMap
        Get
            Return m_showcontrols
        End Get
        Set(ByVal value As Boolean)
            m_showcontrols = value
        End Set
    End Property
    Public WriteOnly Property AutoResize() As Boolean Implements IMapControl.AutoResize
        Set(ByVal value As Boolean)
            m_autoresize = value
        End Set
    End Property

#End Region

    Private Sub MapViewSummary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblMessage.Dock = DockStyle.Fill
        ShowMessage("no map")
        If DesignMode Then Return
        WebBrowser1.ObjectForScripting = Me
        'WebBrowser1.ScriptErrorsSuppressed = True
    End Sub

    Public Sub PanTo(ByVal latitude As Double, ByVal longitude As Double)
        VirtualEarthMap.PanTo(latitude, longitude, WebBrowser1.Document)
    End Sub

    Private Sub m_connection_ConnectionChanged() Handles m_connection.ConnectionChanged
        WebBrowser1.Url = Nothing
    End Sub

    Private Sub m_album_mgr_AlbumLoaded() Handles m_album_mgr.AlbumLoaded
        If Not Visible Then Return
        UpdateMap(False)
    End Sub

    Private Delegate Sub DlgUpdateMap(ByVal recreatemap As Boolean)
    Public Sub UpdateMap(ByVal recreatemap As Boolean)
        If InvokeRequired Then
            Invoke(New DlgUpdateMap(AddressOf UpdateMap), recreatemap)
        Else
            If m_map_loaded And Not recreatemap Then
                LoadPhotos()
            Else
                ShowMessage("loading Virtual Earth")
                RaiseEvent LoadingWebpage()
                WebBrowser1.Url = New Uri( _
                    VirtualEarthMap.CreateWebPage(m_album_mgr.Photos, m_showcontrols, _
                                                  WebBrowser1.Width, WebBrowser1.Height))
            End If
        End If
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        'Trace.WriteLine("$$$ document loaded: " & e.Url.ToString & " -- " & e.Url.OriginalString)
        'Threading.Thread.Sleep(1000)
        'Trace.WriteLine("$$$ about to load photos")
        LoadPhotos()
        m_map_loaded = True
        ShowMessage("")
    End Sub

    Private Delegate Sub DlgLoadPhotos()
    Private Sub LoadPhotos()
        If InvokeRequired Then
            Invoke(New DlgLoadPhotos(AddressOf LoadPhotos))
        Else
            ShowMessage("loading photo locations into map")
            RaiseEvent LoadingPhotos()
            VirtualEarthMap.LoadPhotos(m_album_mgr.Photos, WebBrowser1.Document)
            RaiseEvent LoadComplete()
            ShowMessage("")
        End If
    End Sub

    ' called by JavaScript code in the web page
    Public Sub OnPhotoClick(ByVal VE_shapeid As String)
        If Not VirtualEarthMap.Photos.ContainsKey(VE_shapeid) Then Return
        Dim row As DataRow = VirtualEarthMap.Photos.Item(VE_shapeid)
        RaiseEvent PhotoClicked(VE_shapeid, row)
    End Sub

    ' called by JavaScript code in the web page
    Public Sub OnPhotoDoubleClick(ByVal VE_shapeid As String)
        If Not VirtualEarthMap.Photos.ContainsKey(VE_shapeid) Then Return
        Dim row As DataRow = VirtualEarthMap.Photos.Item(VE_shapeid)
        RaiseEvent PhotoDoubleClicked(VE_shapeid, row)
    End Sub

    ' called by JavaScript code in the web page
    Public Sub OnWebpageError(ByVal message As String, ByVal url As String, ByVal line As Integer)
        RaiseEvent WebpageError(message, url, line)
    End Sub

    Private Delegate Sub DlgShowMessage(ByVal msg As String)
    Private Sub ShowMessage(ByVal msg As String)
        If InvokeRequired Then
            Invoke(New DlgShowMessage(AddressOf ShowMessage), msg)
        Else
            Dim bmsg As Boolean = (msg <> "")
            If bmsg Then
                lblMessage.Text = msg
                Dim w As Integer = lblMessage.Width
                Dim h As Integer = lblMessage.Height
                lblMessage.SetBounds((Me.Width - w) / 2, (Me.Height - h) / 2, w, h)
            End If
            lblMessage.Visible = bmsg
            WebBrowser1.Visible = Not bmsg
        End If
    End Sub

    Private m_ignore_resize As Boolean = False

    ' resizes, but only regenerates map if m_autoresize is set
    Public Sub SetSize(ByVal width As Integer, ByVal height As Integer)
        If Me.Width = width And Me.Height = height Then Return
        m_ignore_resize = True
        Me.Width = width
        Me.Height = height
        WebBrowser1.Width = Me.Width
        WebBrowser1.Height = Me.Height
        WebBrowser1.Dock = DockStyle.Fill
        WebBrowser1.ScrollBarsEnabled = False
        m_ignore_resize = False
        If m_map_loaded Then UpdateMap(m_autoresize)
    End Sub

    Private Sub ResizeMap()
        m_ignore_resize = True
        WebBrowser1.Width = Me.Width
        WebBrowser1.Height = Me.Height
        WebBrowser1.Dock = DockStyle.Fill
        WebBrowser1.ScrollBarsEnabled = False
        m_ignore_resize = False
        If m_map_loaded Then UpdateMap(True)
    End Sub

    Private Sub VirtualEarthMapControl_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If m_ignore_resize Then Return
        If m_autoresize Then ResizeMap()
    End Sub
End Class
